<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户名称">
                <a-input allowClear placeholder="请输入用户名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号码">
                <a-input allowClear placeholder="请输入手机号码" v-model="queryParam.mobile"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button value="0"> 待审核 </a-radio-button>
          <a-radio-button value="1"> 已审核 </a-radio-button>
          <a-radio-button value="2"> 已拒绝 </a-radio-button>
        </a-radio-group>
        <a-dropdown>
          <a-menu slot="overlay">
            <a-menu-item @click="handleAll('verify', '1')">批量同意</a-menu-item>
            <a-menu-item @click="handleAll('verify', '2')">批量拒绝</a-menu-item>
            <a-menu-item @click="handleAll('change', '1')" v-show="queryParam.status === '1'">批量显示</a-menu-item>
            <a-menu-item @click="handleAll('change', '2')" v-show="queryParam.status === '1'">批量隐藏</a-menu-item>
            <a-menu-item @click="handleAll('remove')">批量删除</a-menu-item>
          </a-menu>
          <a-button> 批量操作 </a-button>
        </a-dropdown>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :customRow="customRow"
        :scroll="{ x: 'max-content' }"
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="content" slot-scope="text, record">
          <div class="content">
            <div class="img">
              <a-avatar shape="square" :size="48" icon="user" :src="record.avatar" />
            </div>

            <div class="txt">
              <h3>{{ record.name }}</h3>
              <p>{{ record.mobile }}</p>
            </div>
          </div>
        </span>

        <span slot="status" slot-scope="text, record">
          <a-badge :color="getStatus(record.status)[1]" :text="getStatus(record.status)[0]" />
        </span>

        <span slot="created_at" slot-scope="text, record">
          {{ record.created_at > 0 ? $moment(record.created_at * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>

        <span slot="is_show" slot-scope="text, record">
          <a-tag
            v-if="isShow(record.is_show)"
            :color="isShow(record.is_show).color"
            @click="handleItem('change', record)"
          >
            {{ isShow(record.is_show).statusText }}
          </a-tag>
        </span>

        <span slot="action" slot-scope="text, record">
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item v-if="record.status === '0'"
                ><a href="javascript:;" @click="handleItem('verify', record, '1')">同意</a></a-menu-item
              >
              <a-menu-item v-if="record.status === '0'">
                <a href="javascript:;" @click="handleItem('verify', record, '2')">拒绝</a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" @click="handleItem('remove', record)">删除</a>
              </a-menu-item>
            </a-menu>
            <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { videoGoodsAuditAuthor, videoGoodsAuthorList, videoGoodsHandleAuthor } from '@/api/videoClassify'

const columns = [
  {
    title: '用户ID',
    dataIndex: 'user_id',
    width: 100,
  },
  {
    title: '作者信息',
    scopedSlots: {
      customRender: 'content',
    },
    width: 300,
  },
  {
    title: '获赞',
    dataIndex: 'like_count',
    scopedSlots: {
      customRender: 'like_count',
    },
    sorter: (a, b) => a.like_count - b.like_count,
  },
  {
    title: '收藏',
    dataIndex: 'collect_count',
    scopedSlots: {
      customRender: 'collect_count',
    },
    sorter: (a, b) => a.collect_count - b.collect_count,
  },
  {
    title: '粉丝',
    dataIndex: 'fans_count',
    scopedSlots: {
      customRender: 'fans_count',
    },
    sorter: (a, b) => a.fans_count - b.fans_count,
  },
  {
    title: '关注',
    dataIndex: 'focus_count',
    scopedSlots: {
      customRender: 'focus_count',
    },
    sorter: (a, b) => a.focus_count - b.focus_count,
  },
  {
    title: '状态',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '显示',
    scopedSlots: {
      customRender: 'is_show',
    },
  },
  {
    title: '发布时间',
    dataIndex: 'created_at',
    scopedSlots: {
      customRender: 'created_at',
    },
    sorter: (a, b) => a.created_at - b.created_at,
    width: 200,
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  data() {
    return {
      statusList: [
        { id: '-1', name: '全部', color: 'blue', statusText: '-' },
        { id: '1', name: '已开启', color: 'green', statusText: '已开启' },
        { id: '0', name: '已关闭', color: 'red', statusText: '已关闭' },
      ],
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,

      columns,
      queryParam: {
        name: '',
        mobile: '',
        status: '0',
      },
      list: [],
      selectedRowKeys: [],
    }
  },

  mounted() {
    this.getList()
  },

  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (status) {
          case '0':
            text = '未审核'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case '1':
            text = '已通过'
            color = '#65C817'
            break

          case '2':
            text = '已拒绝'
            color = '#FF4D4F'
            break
        }
        return [text, color]
      }
    },

    isShow() {
      return (is_show) => {
        let item = this.statusList.find((el) => el.id === is_show)
        return item
      }
    },
  },

  methods: {
    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await videoGoodsAuthorList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.selectedRowKeys = []
        let { totalCount, data } = res.data
        this.total = totalCount
        this.list = data

        console.log('data,', data)
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 选中多行
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    // 批量操作
    handleAll(type, status) {
      let { selectedRowKeys, list } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      switch (type) {
        case 'verify':
          let newList = list
            .filter((el) => {
              return selectedRowKeys.indexOf(el.id) !== -1 && el.status === '0'
            })
            .map((el) => el.id)

          if (!newList.length) {
            this.$message.error('请选择符合条件的数据~')
            return
          }

          this.verify(newList, status)
          break

        case 'change':
          let statusList = list
            .filter((el) => {
              return selectedRowKeys.indexOf(el.id) !== -1 && el.is_show !== status
            })
            .map((el) => el.id)

          if (!statusList.length) {
            this.$message.error('请选择符合条件的数据~')
            return
          }

          this.changeStatus('1', statusList, status)
          break

        case 'remove':
          this.changeStatus('2', newList, status)
          break
      }
    },

    // 点击单个
    handleItem(type, item, status) {
      switch (type) {
        case 'verify':
          this.verify([item.id], status)
          break

        case 'change':
          let is_show = '1'
          if (item.is_show === '1') {
            is_show = '0'
          }
          this.changeStatus('1', [item.id], is_show)
          break

        case 'remove':
          this.changeStatus('2', [item.id], '1')
          break
      }
    },

    verify(id, status) {
      videoGoodsAuditAuthor({
        id,
        status,
      }).then((res) => {
        if (res.code !== 0) {
          this.$error({
            content: res.msg,
          })
        } else {
          this.$message.success(res.msg)

          if (id.length > 1) {
            this.getList()
          } else {
            let idItem = id[0],
              item = this.list.find((el) => el.id === idItem),
              index = this.list.findIndex((el) => el.id === idItem)
            if (item) {
              // if (this.queryParam.status !== '-1') {
              this.list.splice(index, 1)
              // return
              // }
              // this.$set(item, 'status', status)
            }
          }
        }
      })
    },

    // 修改状态(删除接口)
    changeStatus(type, id, status) {
      videoGoodsHandleAuthor({
        id,
        type,
        status,
      }).then((res) => {
        if (res.code !== 0) {
          this.$error({
            content: res.msg,
          })
        } else {
          this.$message.success(res.msg)

          if (type === '1') {
            if (id.length > 1) {
              this.getList()
            } else {
              let idItem = id[0],
                item = this.list.find((el) => el.id === idItem)

              if (item) {
                this.$set(item, 'is_show', status)
              }
            }
          } else {
            this.list = this.list.filter((el) => id.indexOf(el.id) === -1)
            this.totalCount -= id.length
            if (!this.list.length) {
              this.getList()
            }
          }

          this.selectedRowKeys = []
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

::v-deep {
  .ant-tag {
    cursor: pointer;
  }
}
</style>
